package com.cbank.dao;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;
import com.cbank.domain.AdminUser;
import com.cbank.domain.BaseEntity;

@Repository
public interface AdminUserDao extends BaseDao<AdminUser, String, Integer>{
	public static final String ALL = "id,user_name,password,token,type,view_id,real_name,status,create_time,update_time";
	
	@Override
	@Insert("insert into admin_user (user_name,password,type,view_id,real_name,status) values(#{userName},#{password},#{type},#{viewId},#{realName},#{status})")
	@Options(useGeneratedKeys = true)
	Integer insert(AdminUser adminUser);

	@Override
	@Update("update admin_user set user_name=#{userName},password=#{password},type=#{type},real_name=#{realName} where id = #{id}")
	Integer updateById(AdminUser adminUser);

	@Override
	@Select("select " + ALL + " from admin_user where id = #{id}")
	AdminUser findById(@Param("id")Integer id);

	@Override
	@Select("select " + ALL + " from admin_user where viewId = #{viewId}")
	AdminUser findByViewId(@Param("viewId")String viewId);

	@Override
	@Select("select " + ALL + " from admin_user where status = " + BaseEntity.STATUS_VALID)
	List<AdminUser> findAll();
	
	/**
	 * 根据用户名和密码查询用户
	 * @param adminUser
	 * @return AdminUser
	 */
	@Select("select " + ALL + " from admin_user where user_name=#{userName} and password=#{password}")
	AdminUser findByUserNameAndPassword(AdminUser adminUser);
	
	/**
	 * 用户登录更新token
	 * @param token
	 * @param id
	 * @return
	 */
	@Update("update admin_user set token=#{token} where id = #{id}")
	Integer updateTokenById(@Param("token")String token,@Param("id")Integer id);
	

}
